package com.cxj.link.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
import com.cxj.link.util.ImageUtil;
import com.cxj.link.vo.LinkVo;

import java.util.List;

public class GameView extends View {
    private GameLogic gameService;
    private LinkVo linkInfo;
    private Paint paint = new Paint();
    private Cut[] promptPiece;
    private Bitmap selectImage;
    private Bitmap selectImage2;
    private Cut selectedPiece;

    public GameView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        this.paint.setStrokeWidth(3.0F);
        this.paint.setColor(-65536);
        this.paint.setStrokeWidth(3.0F);
        this.selectImage = ImageUtil.getSelectImage(paramContext);
        this.selectImage2 = ImageUtil.getSelectImage2(paramContext);
    }

    private void drawLine(LinkVo paramLinkInfo, Canvas paramCanvas) {
        List localList = paramLinkInfo.getLinkPoints();
        for (int i = 0; i < localList.size() - 1; i++) {
            Point localPoint2 = (Point) localList.get(i);
            Point localPoint1 = (Point) localList.get(i + 1);
            paramCanvas.drawLine(localPoint2.x, localPoint2.y, localPoint1.x, localPoint1.y, this.paint);
        }
    }

    @Override
    protected void onDraw(Canvas paramCanvas) {
        super.onDraw(paramCanvas);
        if (this.gameService != null) {
            Cut[][] arrayOfPiece = this.gameService.getPieces();
            if (arrayOfPiece != null)
                for (int j = 0; j < arrayOfPiece.length; j++)
                    for (int i = 0; i < arrayOfPiece[j].length; i++) {
                        if (arrayOfPiece[j][i] == null)
                            continue;
                        Cut localPiece = arrayOfPiece[j][i];
                        paramCanvas.drawBitmap(localPiece.getImage().getImage(), localPiece.getBeginX(),
                                localPiece.getBeginY(), null);
                    }
            if (this.linkInfo != null) {
                drawLine(this.linkInfo, paramCanvas);
                this.linkInfo = null;
                this.promptPiece = null;
                postInvalidate();
            }
            if ((this.promptPiece != null) && (this.promptPiece[0] != null) && (this.promptPiece[1] != null)) {
                paramCanvas.drawBitmap(this.selectImage2, this.promptPiece[0].getBeginX(),
                        this.promptPiece[0].getBeginY(), null);
                paramCanvas.drawBitmap(this.selectImage2, this.promptPiece[1].getBeginX(),
                        this.promptPiece[1].getBeginY(), null);
            }
            if (this.selectedPiece != null)
                paramCanvas.drawBitmap(this.selectImage, this.selectedPiece.getBeginX(),
                        this.selectedPiece.getBeginY(), null);
        }
    }

    public void setGameService(GameLogic paramGameService) {
        this.gameService = paramGameService;
    }

    public void setLinkInfo(LinkVo paramLinkInfo) {
        this.linkInfo = paramLinkInfo;
    }

    public void setPromptPiece(Cut[] paramArrayOfPiece) {
        this.promptPiece = paramArrayOfPiece;
    }

    public void setSelectedPiece(Cut paramPiece) {
        this.selectedPiece = paramPiece;
    }

    public void startGame() {
        this.gameService.start();
        postInvalidate();
    }
}
